﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalleryCommon;

namespace GalleryDataProvider
{
    public static class AlbumDataProvider
    {
        private static readonly SqlConnection myConnection = ConnectionProvider.Connection;

        public static bool CreateAlbum(string albumName, long ownerId)
        {
            bool result = true;

            SqlCommand command = new SqlCommand("insert into Albums (Name, Date, OwnerID) values (@Name, @Date, @OwnerID)", myConnection);
            command.Parameters.AddWithValue("Name", albumName);
            command.Parameters.AddWithValue("Date", DateTime.Now);
            command.Parameters.AddWithValue("OwnerID", ownerId);

            try
            {
                command.ExecuteNonQuery();

            }
            catch (SqlException)
            {
                result = false;
            }
            return result;
        }

        public static bool GetAlbums(long ownerId, out List<Album> albums)
        {
            bool result = true;

            SqlCommand command = new SqlCommand("select AlbumID, Name, Date, Description from Albums where OwnerID = @OwnerID", myConnection);
            command.Parameters.AddWithValue("OwnerID", ownerId);

            albums = new List<Album>();

            try
            {
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    if (reader.HasRows)
                    {
                        albums.Add(
                            new Album
                            {
                                Date = reader.GetDateTime(2),
                           //     Description = reader.GetString(3),
                                ID = reader.GetInt64(0),
                                Name = reader.GetString(1),
                                OwnerID = ownerId
                            });
                    }
                }
                reader.Close();
            }
            catch (SqlException)
            {
                result = false;
            }
            
            return result;
        }

        /// <summary>
        /// Lekérdezi az album nevét és a benne szereplő képeket
        /// </summary>
        /// <param name="id">Az album azonosítója</param>
        /// <returns>Egy Folder példány, melyben Picture példány referenciák vannak</returns>
        public static Folder GetFolderByID(long id)
        {
            Folder f = new Folder();

            SqlCommand command = new SqlCommand("select AlbumID, Name, OwnerID, Date from Albums where AlbumID = @ID", myConnection);
            command.Parameters.AddWithValue("ID", id);
            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                reader.Read();

                f.FolderID = (long)(reader.GetValue(0));
                f.Title = (string)(reader.GetValue(1));
                f.OwnerID = (long)(reader.GetValue(2));
                f.Date = (DateTime)(reader.GetValue(3));

                PictureDataProvider.GivePicture(f.FolderID, out f.Pictures,1);
            }
            else return new Folder { Title="ILYEN AZONOSÍTÓVAL NEM RENDELKEZIK" }; //teszteléshez

            return f;
        }
    }
}
